layui.use(['table','jquery'], function(){
    var table = layui.table;
    var $ = layui.jquery;
    var form=layui.form;
    table.render({
      elem: '#menu'
      ,url:'./getmenu',
      method:"post"
      ,toolbar: '#toolbarDemo'
      ,title: '用户数据表',
      limits:[3,6,9,12],
      limit:3,
    //   height: 310,
      id:"testReload",
      cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field:'m_id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
        ,{field:'m_name', title:'菜单名字', width:120, edit: 'text'}
        ,{field:'m_link', title:'链接', width:150, edit: 'text'}
        ,{field:'m_num', title:'编号', width:80, edit: 'text', sort: true}
        ,{field:'m_status', title:'状态', width:100,templet: '#switchTpl', unresize: true}
        ,{field:'m_parent', title:'父级菜单',edit: 'text'}
        ,{field:'m_icon', title:'图标', width:80, sort: true,edit: 'text'}
        ,{field:'m_url', title:'网址', width:120,edit: 'text'}
        ,{field:'m_orderby', title:'顺序', width:100, sort: true,edit: 'text'}
        ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
      ]],
      page: true
    });  
    //头工具栏事件
    table.on('toolbar(menu)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
          var data = checkStatus.data;
          layer.open({
            type: 2,
            title: '添加菜单',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '550px'],
            content: './addmenu'
          });
        break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
          layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
      };
    });
    
    //监听行工具事件
    table.on('tool(menu)', function(obj){
      var data = obj.data;
      //console.log(obj)
      if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
            
            $.ajax({
                url:"./deleteIdmenu",
                data:{mId:data.m_id},
                method:"post",
                dataType:"json"
            }).then(function(res)
            {
                console.log(res.status);
                if(res.status==1){
                     obj.del();
                     layer.alert(res.msg, {
                        skin: 'layui-layer-molv' //样式类名
                            ,
                        closeBtn: 0
                    });
                }
            })
         
        });
      } 
    });
     
  //监听单元格编辑
  table.on('edit(menu)', function(obj){
    var value = obj.value; //得到修改后的值
    console.log(obj.data)
    // ,data = obj.data //得到所在行所有键值
    // ,field = obj.field; //得到字段
    $.ajax({
        url:"./updateidmenu",
        data:obj.data,
        method:"post",
        dataType:"json",
    }).then(function(res)
    {
        if (res.status == 1) {

            layer.alert(res.msg, {
                skin: 'layui-layer-lan',
                closeBtn: 0,
                anim: 4 //动画类型
            });

        } else {
                layer.alert(res.msg, {
                skin: 'layui-layer-molv' //样式类名
                    ,
                closeBtn: 0
            });
            
        }    })
   
  });

  //提交
  form.on('switch(m_status)', function(obj){
    obj.status=this.checked==true?1:0;
    console.log(obj.value);
    data={
        sta:obj.status,
        mId:obj.value
    };
    $.ajax({
        url:"./updateidstatus",
        data:data,
        method:"post",
        dataType:"json",
    }).then(function(res)
    {
        if (res.status == 1) {

            layer.alert(res.msg, {
                skin: 'layui-layer-lan',
                closeBtn: 0,
                anim: 4 //动画类型
            });

        } else {
            console.log(res);
                layer.alert(res.msg, {
                skin: 'layui-layer-molv' //样式类名
                    ,
                closeBtn: 0
            });
            
        }    })

   });

   let active = {
    reload: function(){
      var demoReload = $('#demoReload');
      table.reload('testReload', {
        page: {
          curr: 0//重新从第 1 页开始
        }
        ,where: {
            key: {
                m_id: demoReload.val()
              }
        
        }
      });
    //   let mId=demoReload.val();
    //   $.ajax({
    //     url:"./searchmenu",
    //     data:{mId:mId},
    //     method:"post",
    //     dataType:"json",
    //   }).then(function(res)
    //   {
    //     console.log(res[0]);
    //     for(var i in res[0])
    //     {
    //         console.log(res[0][i]);
            
    //     }
    // })
      //执行重载
    
    }
  };
  active.reload();
  $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    // console.log(active[type].call($(this)));
    active[type] ? active[type].call(this) : '';
    // active[type] ? active[type]: '';
  });
  });